package cn.wen.yinxiu.member.service.impl;

import cn.wen.yinxiu.common.enums.DelEnum;
import cn.wen.yinxiu.common.enums.StatusEnum;
import cn.wen.yinxiu.member.vo.UserFriend;
import cn.wen.yinxiu.member.vo.UserFriendInfo;
import cn.wen.yinxiu.member.vo.UserFriendQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wen.yinxiu.common.toolkit.PageUtils;
import cn.wen.yinxiu.common.toolkit.Query;

import cn.wen.yinxiu.member.dao.FriendDao;
import cn.wen.yinxiu.member.entity.FriendEntity;
import cn.wen.yinxiu.member.service.FriendService;
import org.springframework.transaction.annotation.Transactional;


@Service("friendService")
public class FriendServiceImpl extends ServiceImpl<FriendDao, FriendEntity> implements FriendService {

    @Value("${yinxiu.pageSize}")
    private Integer pageSize;

    @Autowired
    private FriendDao friendDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FriendEntity> page = this.page(
                new Query<FriendEntity>().getPage(params),
                new QueryWrapper<FriendEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<Long> queryUserFriendIdList(Long userId) {
        return friendDao.selectFriendIdByUserId(userId,
                StatusEnum.NORMAL_USE.code(), DelEnum.NORMAL.code());
    }

    @Override
    public List<UserFriendInfo> queryUserFriendInfoList(Long userId, Integer page) {
        return friendDao.selectFriendInfoByUserId(userId,
                StatusEnum.NORMAL_USE.code(), DelEnum.NORMAL.code(), page, pageSize);
    }

    @Override
    public FriendEntity queryUserFriendById(Long userId, Long friendId) {
        QueryWrapper<FriendEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("me_id", userId);
        queryWrapper.eq("friend_id", friendId);
        queryWrapper.eq("is_deleted", DelEnum.NORMAL.code());
        return friendDao.selectOne(queryWrapper);
    }

    @Transactional
    @Override
    public void addUserFriend(List<UserFriend> userFriendList) {
        // 1.需要先删除已存在的数据
        for (UserFriend userFriend : userFriendList) {
            QueryWrapper<FriendEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("me_id", userFriend.getUserId());
            queryWrapper.eq("friend_id", userFriend.getUserFriendId());
            friendDao.delete(queryWrapper);
        }
        // 2.批量添加
        friendDao.saveBatchUserFriend(userFriendList);
    }

    @Override
    public List<UserFriendInfo> queryUserFansInfoList(Long userId, Integer page) {
        return friendDao.selectFansInfoByUserId(userId,
                StatusEnum.NORMAL_USE.code(), DelEnum.NORMAL.code(), page, pageSize);
    }

    @Override
    public boolean deleteUserFriendFromFansRelation(Long userId, Long fansId) {
        QueryWrapper<FriendEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("me_id", fansId);
        queryWrapper.eq("friend_id", userId);
        FriendEntity target = new FriendEntity();
        target.setIsDeleted(DelEnum.DELETE.code());
        return friendDao.update(target, queryWrapper) > 0;
    }

    @Override
    public boolean deleteUserFriendRelation(Long userId, Long friendId) {
        QueryWrapper<FriendEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("me_id", userId);
        queryWrapper.eq("friend_id", friendId);
        FriendEntity target = new FriendEntity();
        target.setIsDeleted(DelEnum.DELETE.code());
        return friendDao.update(target, queryWrapper) > 0;
    }

    @Override
    public List<UserFriendInfo> searchUserFriendInfoList(UserFriendQuery query) {
        return friendDao.searchUserFriendInfoList(query,
                StatusEnum.NORMAL_USE.code(), DelEnum.NORMAL.code(),
                pageSize * (query.getPage() - 1), pageSize);
    }

    @Override
    public List<UserFriendInfo> searchUserFansInfoList(UserFriendQuery query) {
        return friendDao.searchUserFansInfoList(query,
                StatusEnum.NORMAL_USE.code(), DelEnum.NORMAL.code(),
                pageSize * (query.getPage() - 1), pageSize);
    }

    @Override
    public List<Long> queryFriendIdList(Long userId) {
        return friendDao.queryUserFriendIdList(userId, StatusEnum.NORMAL_USE.code(), DelEnum.NORMAL.code());
    }

    @Override
    public boolean checkUserFriend(Long userId, Long friendId) {
        QueryWrapper<FriendEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("me_id", userId);
        queryWrapper.eq("friend_id", friendId);
        queryWrapper.eq("is_deleted", DelEnum.NORMAL.code());
        return friendDao.selectOne(queryWrapper) != null;
    }

}